package com.hxc.shop.controller;

import com.hxc.shop.bean.ShopUserInfo;
import com.hxc.shop.bean.Vo.UserRegistrationVo;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.service.UserService;
import io.swagger.annotations.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = {"用户信息处理"})
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/userRegistration")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO userRegistration(@RequestBody UserRegistrationVo user) {
        return userService.userRegistration(user);
    }

    @ApiOperation("用户登录")
    @GetMapping("/userLogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名",required = true,paramType = "string"),
            @ApiImplicitParam(name="passWord",value = "密码",required = true,paramType = "string"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO userLogin(@RequestParam("userName") String userName,@RequestParam("passWord") String passWord) {
        return userService.userLogin(userName,passWord);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/userInfo")
    @Transactional(rollbackFor = Exception.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名",required = true,paramType = "string"),
    })
    public ResultVO userInfo(@RequestParam("userName")String userName) {
        return userService.userInfo(userName);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/changeUserInfo")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO changeUserInfo(@RequestBody ShopUserInfo user) {
        return userService.changeUserInfo(user);
    }


    @ApiOperation("修改用户密码")
    @GetMapping("/changeUserPassWord")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名",required = true,paramType = "string"),
            @ApiImplicitParam(name="passWord",value = "密码",required = true,paramType = "string"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO changeUserPassWord(@RequestParam("userName")String userName,@RequestParam("passWord")String passWord) {
        return userService.changeUserPassWord(userName,passWord);
    }

    @ApiOperation("充值余额")
    @GetMapping("/chargeUserBlance")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名",required = true,paramType = "string"),
            @ApiImplicitParam(name="blance",value = "需要充值的金额",required = true,paramType = "Double"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO chargeUserBlance(@RequestParam("userName")String userName,@RequestParam("blance")Double blance) {
        return userService.chargeUserBlance(userName,blance);
    }

    @ApiOperation("将用户修改为管理员/普通用户")
    @GetMapping("/updateUserAuth")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名",required = true,paramType = "string"),
            @ApiImplicitParam(name="isAdmin",value = "用户权限，1是管理员，0不是",required = true,paramType = "Integer"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO updateUserAuth(@RequestParam("userName")String userName,@RequestParam("isAdmin")Integer isAdmin) {
        return userService.updateUserAuth(userName,isAdmin);
    }

    @ApiOperation("删除用户")
    @GetMapping("/deleteUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "用户名",required = true,paramType = "string"),
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO deleteUser(@RequestParam("userName")String userName) {
        return userService.deleteUser(userName);
    }
}
